window.onload = function () {
    const voiceTxt = document.querySelector('#chat-input');
    const startBtn = document.querySelector('#start-btn');
    const fixedBox = document.querySelector('#fixed-box');
    const fixedTxt = document.querySelector('#fixed-txt');
    const closeBtn = document.querySelector('#close-btn');
    let times = null;

    // 设置按钮样式
    startBtn.style.border = "none";
    startBtn.style.backgroundColor = "#116682";

    const xfVoice = new XfVoiceDictation({
        APPID: 'af680f76',
        APISecret: 'N2JlMDlkZWU2YmU1NzlkMTQzZWZhZTIy',
        APIKey: '890c817ea9a76b4ac68285714d30a3e2',
        onWillStatusChange: function (oldStatus, newStatus) {
            fixedBox.style.display = 'block';
            console.log('识别状态：', oldStatus, newStatus);
        },
        onTextChange: function (text) {
            voiceTxt.value = text;
            fixedTxt.innerText = text;
            console.log('识别内容：',text)
            // 3秒钟内没有说话，就自动关闭
            if (text) {
                clearTimeout(times);
                times = setTimeout(() => {
                    this.stop();
                    fixedBox.style.display = 'none';
                }, 3000);
            }
        }
    });

    // 开始识别
    startBtn.onclick = function () {
        xfVoice.start();
    };

    // 关闭识别
    if (closeBtn) {
        closeBtn.onclick = function () {
            xfVoice.stop();
            fixedBox.style.display = 'none';
        };
    }
}; 